package com.taptech.doufu.ui.fragment.NovelChildFragments;

import android.content.Context;
import android.graphics.Paint;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.ui.activity.NovelSectionDetailsActivity;
import com.taptech.doufu.bean.HomeTopBean;
import com.taptech.doufu.util.TextUtil;
import com.taptech.doufu.ui.view.theme.read.ReadThemeHelper;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class ReadBookUIUtil {


    /**
     * 设置两个按钮的样式
     * @param context
     * @param btnPay
     * @param btnPayAll
     */
    public static void setBtnUI(Context context,Button btnPay, FrameLayout btnPayAll,LinearLayout llPayAllTip,TextView tvPayAllTip,TextView tvPayAllTipDesc) {
        //按钮UI调整
        if (btnPay.getVisibility() == View.VISIBLE && btnPayAll.getVisibility() == View.VISIBLE) {
            //设置UI
            btnPay.setTextColor(context.getResources().getColor(ReadThemeHelper.getReadTheme().getPayTheme().getBtn2FontColor()));
            btnPay.setBackgroundResource(ReadThemeHelper.getReadTheme().getPayTheme().getBtn2Bg());
        } else if (btnPay.getVisibility() == View.VISIBLE && btnPayAll.getVisibility() != View.VISIBLE) {
            //设置UI
            btnPay.setTextColor(context.getResources().getColor(ReadThemeHelper.getReadTheme().getPayTheme().getBtnFontColor()));
            btnPay.setBackgroundResource(ReadThemeHelper.getReadTheme().getPayTheme().getBtnBg());
        }
        tvPayAllTip.setTextColor(context.getResources().getColor(ReadThemeHelper.getReadTheme().getPayTheme().getBtnFontColor()));
        tvPayAllTipDesc.setTextColor(context.getResources().getColor(ReadThemeHelper.getReadTheme().getPayTheme().getBtnFontColor()));
        btnPayAll.setBackgroundResource(ReadThemeHelper.getReadTheme().getPayTheme().getBtnBg());
        llPayAllTip.setBackgroundResource(ReadThemeHelper.getReadTheme().getPayTheme().getBtnBg());

    }

    /**
     * 设置布局
     * @param activity
     * @param topicBean
     * @param contentBean
     * @param btnPayAll
     * @param llBuyAllTip
     * @param tvTip
     * @param btnPay
     * @param ivLimitTime
     * @param tvPayAllTip
     * @param llAutoPay
     * @param tvAllPrice
     */
    public static void setPayLayout(NovelSectionDetailsActivity activity,
                                    HomeTopBean topicBean,
                                    HomeTopBean contentBean,
                                    FrameLayout btnPayAll,
                                    LinearLayout llBuyAllTip,
                                    TextView tvTip,
                                    Button btnPay,
                                    ImageView ivLimitTime,
                                    LinearLayout llPayAllTip,
                                    TextView tvPayAllTip,
                                    TextView tvPayAllTipDesc,
                                    LinearLayout llAutoPay,
                                    TextView tvAllPrice) {
        if(topicBean!=null && Integer.parseInt(topicBean.getEnd_price())>0){  //完本
            //完本按钮
            btnPayAll.setVisibility(View.VISIBLE);
            //价格提示
            llBuyAllTip.setVisibility(View.VISIBLE);
            float allBookPrice = Float.parseFloat(topicBean.getEnd_price()); //完本购买所需的豆币
            float verbPrice = Float.parseFloat(topicBean.getTotal_price());  //剩余所需的豆币
            if (topicBean.getShort_novel() > 0 ||
                    activity !=null && activity.getPayInfoBean()!=null &&
                            activity.getPayInfoBean().getPurchasedTotalHalf()==0){  //短篇
                tvTip.setText("本作品按本购买，需一次性购买阅读");
                btnPay.setVisibility(View.GONE);
            }else{  //非短篇
                //单章节购买按钮  1.阅读券购买  2.豆币购买
                btnPay.setVisibility(View.VISIBLE);
                setPriceText(activity,contentBean,tvTip,btnPay);
            }
            //限时购买图标
            if (topicBean.getFull_activity() != null) { //特殊情况 ，限时活动
                ivLimitTime.setVisibility(View.VISIBLE);
                //限时活动，使用活动价
                String price = topicBean.getFull_activity().getActivity_price();
                if (!TextUtils.isEmpty(price)) {
                    String[] number = price.split("\\.");
                    if (number.length > 0) {
                        try {
                            allBookPrice = Float.parseFloat(number[0]);
                        } catch (Exception e) {
                        }
                    }
                }
            }else{
                ivLimitTime.setVisibility(View.GONE);
            }
            float saveCoin = 0;

            if (topicBean.getShort_novel() > 0 ||
                    activity != null && activity.getPayInfoBean() != null &&
                            activity.getPayInfoBean().getPurchasedTotalHalf() == 0) {   //一次性购买
                if (activity != null && activity.getPayInfoBean() != null && activity.getPayInfoBean().isNewVip()) {
                    saveCoin = (allBookPrice*(1-activity.getPayInfoBean().getDiscount()));
                    allBookPrice = (allBookPrice*activity.getPayInfoBean().getDiscount());
                }else{
                    saveCoin = 0;
                }
                tvPayAllTip.setText("购买完本  " + formatPrice(allBookPrice) + "豆币");
            }else{
                if(activity!=null && activity.getPayInfoBean()!=null) {  //会员
                    if (activity.getPayInfoBean().getDiscount() < 1 && activity.getPayInfoBean().getDiscount() > 0) {
                        verbPrice = (activity.getPayInfoBean().getSurplusTotal() * activity.getPayInfoBean().getDiscount());  //会员打折
                        saveCoin = (activity.getPayInfoBean().getSurplusTotal() * (1 - activity.getPayInfoBean().getDiscount()));  //会员节省
                    }else{
                        verbPrice = activity.getPayInfoBean().getSurplusTotal();
                    }
                }
                if (activity != null && activity.getPayInfoBean() != null && activity.getPayInfoBean().isNewVip()) {
                    saveCoin = (allBookPrice*(1-activity.getPayInfoBean().getDiscount()));
                    allBookPrice = (allBookPrice*activity.getPayInfoBean().getDiscount());
                }
                if (allBookPrice > verbPrice) { //剩余章节打包
                    tvPayAllTip.setText("剩余章节购买  " + formatPrice(verbPrice) + "豆币");
                }else{  //完本购买
                    tvPayAllTip.setText("购买完本  " + formatPrice(allBookPrice) + "豆币");
                }
            }

            if (saveCoin > 0) {
                tvPayAllTipDesc.setVisibility(View.VISIBLE);
                if (activity.getPayInfoBean().isNewVip()) {
                    tvPayAllTipDesc.setText("(会员已省"+formatPrice(saveCoin)+"豆币)");
                }else{
                    tvPayAllTipDesc.setText("(会员已省"+formatPrice(saveCoin)+"豆币)");
                }
                tvPayAllTip.setTextSize(15);
            }else{
                tvPayAllTip.setTextSize(18);
                tvPayAllTipDesc.setVisibility(View.GONE);
            }
            //自动购买图标不显示
            llAutoPay.setVisibility(View.GONE);
            tvAllPrice.setText(topicBean.getTotal_price());
            tvAllPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        }else{  //正常
            btnPay.setTextColor(activity.getResources().getColor(ReadThemeHelper.getReadTheme().getPayTheme().getBtnFontColor()));
            btnPay.setBackgroundResource(ReadThemeHelper.getReadTheme().getPayTheme().getBtnBg());
            llBuyAllTip.setVisibility(View.GONE);
            btnPayAll.setVisibility(View.GONE);
            if(llAutoPay.getVisibility() != View.VISIBLE){
                llAutoPay.setVisibility(View.VISIBLE);
            }
            setPriceText(activity,contentBean,tvTip,btnPay);

        }
        ReadBookUIUtil.setBtnUI(activity,btnPay,btnPayAll,llPayAllTip,tvPayAllTip,tvPayAllTipDesc);
    }

    private static void setPriceText(NovelSectionDetailsActivity activity,
                                     HomeTopBean contentBean,
                                     TextView tvTip,
                                     Button btnPay) {
        //单章节购买提示
        if(!TextUtil.isEmpty(contentBean.getSpend_num())){
            if(activity!=null && activity.getPayInfoBean()!=null
                    &&activity.getPayInfoBean().getDiscount()< 1 && activity.getPayInfoBean().getDiscount()>0) {  //会员
                tvTip.setText(Html.fromHtml("本章会员价格：<font color='#fe6e6e'>"+contentBean.getSpend_num()+"</font> 豆币（原价"+
                        formatPrice(Float.parseFloat(contentBean.getSpend_num())/activity.getPayInfoBean().getDiscount())+"豆币）"));
            }else{
                tvTip.setText(Html.fromHtml("本章价格：<font color='#fe6e6e'>"+contentBean.getSpend_num()+"</font>豆币，需购买后阅读"));
            }
        }
        if (activity!=null && activity.getPayInfoBean() !=null
                &&activity.getPayInfoBean().getReadTicketInfo()!=null){
            btnPay.setText("使用阅读券购买此章");
        }else {
            btnPay.setText("确定购买此章");
        }
    }

    private static String formatPrice(float price) {
        NumberFormat nf = new DecimalFormat("#.#");
        String str = nf.format(price);
        return str;
    }
}
